
import GreyReactBox from 'grey-react-box';
import devConfig from 'devConfig';

interface IApp {

    /** 应用ID */
    id: string;

    /** 应用地址 */
    url: string;
}

class AppManagementState {

    /** 前台应用 */
    frontDesk: string = '';

    /** 后台应用列表 */
    backstage: string[] = [];

    /** 应用字典 */
    apps = new Map<string, IApp>();
}

/** 应用管理 */
export default class AppManagement extends GreyReactBox<AppManagementState>{

    private static _instance: AppManagement;

    static get instance() {
        return AppManagement._instance || new AppManagement();
    }

    constructor() {
        super(new AppManagementState());
        if (AppManagement._instance) throw new Error("只能实例化一个对象");
        AppManagement._instance = this;
        this.state.frontDesk = window.$_Mic_Config.main;
        window.$_Mic_Config.defaultApp.forEach(e => { this.state.apps.set(e.id, e) }); // 默认应用
    }

    getApp = (id: string) => {
        if (devConfig && devConfig.start && devConfig.apps) {
            return devConfig.apps[id] || this.state.apps.get(id);
        } else {
            return this.state.apps.get(id);
        }
    }
}